﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;

using LIM.forms;
using System.Reflection;

namespace LIM
{
    public partial class Main : RadForm
    {
        public RadDesktopAlert radDesktopAlert_SavingStatus = null;
        public bool IsWarningClosing = true;
        
        public Main()
        {
            InitializeComponent();
            
            ThemeResolutionService.ApplicationThemeName = "Office2010Blue"; //hqd change theme
            
            CreateMenuItemInRibbonBar createMenuItemInRibbonBar = new CreateMenuItemInRibbonBar(this.radRibbonBar1);
            createMenuItemInRibbonBar.CreateStartMenuItems();

            // Create RadDesktopAlert_SavingStatus
            this.radDesktopAlert_SavingStatus = new RadDesktopAlert(this.components);
            this.radDesktopAlert_SavingStatus.Closed += new RadPopupClosedEventHandler(radDesktopAlert_Closed);
            RadDesktopAlert_SavingStatus.Init(this);
            // End RadDesktopAlert_SavingStatus

            ApplyMenuPermissions();
        }
        private void Main_Load(object sender, EventArgs e)
        {
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                System.Deployment.Application.ApplicationDeployment ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
                //AssemblyName myAssemblyName = new AssemblyName();
                //Version version = ;    
            }

            this.radLabelElement_Version.Text = "Version: " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void ExitApplication()
        {
            Application.Exit();
        }

        private void ApplyMenuPermissions()
        {            
            if (InitUserInfos.IsAdmin) // check for user is Admin 
                return;

            // for start menu
            foreach (var item in this.radRibbonBar1.StartMenuItems)
            {
                if (item is RadMenuSeparatorItem)
                    continue;
                else if (item is RadMenuItem)
                {
                    RadMenuItem menuItem = (RadMenuItem)item;
                    if (InitUserInfos.MenuPermissions.Where(s => s.Name == menuItem.Name).FirstOrDefault() != null)
                    {
                        menuItem.Visibility = ElementVisibility.Visible;
                        if (menuItem.HasChildren)
                        {
                            if (InitUserInfos.MenuPermissions.Where(s => s.Name == menuItem.Name).FirstOrDefault() != null)
                            {
                                menuItem.Visibility = ElementVisibility.Visible;
                                if (menuItem.HasChildren)
                                    ApplyPermissionsForStartMenuChilds(menuItem);
                            }
                            else
                                menuItem.Visibility = ElementVisibility.Collapsed;
                        }
                    }
                    else
                    {
                        if (HasStartMenuIsNotApplyPerrmissions(menuItem))
                            continue;

                        menuItem.Visibility = ElementVisibility.Collapsed;
                    }
                }           
            }

            // for command tabs
            foreach (var item in this.radRibbonBar1.CommandTabs)
            {
                if (InitUserInfos.MenuPermissions.Where(s => s.Name == item.Name).FirstOrDefault() != null)
                {
                    item.Visibility = ElementVisibility.Visible;
                }
                else
                    item.Visibility = ElementVisibility.Collapsed;
            }

        }
        
        private void ApplyPermissionsForStartMenuChilds(RadMenuItem parent)
        {
            foreach (var child in parent.Items)
            {
                if (child is RadMenuSeparatorItem)
                    continue;
                else if (child is RadMenuItem)
                {
                    RadMenuItem menuItem = (RadMenuItem)child;
                    if (InitUserInfos.MenuPermissions.Where(s => s.Name == menuItem.Name).FirstOrDefault() != null)
                    {
                        menuItem.Visibility = ElementVisibility.Visible;
                        if (menuItem.HasChildren)
                            ApplyPermissionsForStartMenuChilds(menuItem);
                    }
                    else
                    {
                        if (HasStartMenuIsNotApplyPerrmissions(menuItem))
                            continue;

                        menuItem.Visibility = ElementVisibility.Collapsed;
                    }
                }
            }
        }
        private bool HasStartMenuIsNotApplyPerrmissions(RadMenuItem item)
        {
            string[] menu_arrays = { "radMenuItem_ThongTinCaNhan", "radMenuItem_ThayDoiMatKhau", "radMenuItem_DangXuat" };
            if (menu_arrays.Contains(item.Name))
                return true;
            return false;
        }

        private void radButtonElement_TaoMoiKhachHang_Click(object sender, EventArgs e)
        {
            radForm_CRM_KhachHang_ChiTiet form = new radForm_CRM_KhachHang_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiLienHe_Click(object sender, EventArgs e)
        {
            radForm_CRM_LienHe_ChiTiet form = new radForm_CRM_LienHe_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachKhachHang_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachKhachHang form = new radForm_CRM_DanhSachKhachHang();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachLienHe_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachLienHe form = new radForm_CRM_DanhSachLienHe();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_NhatKyLienLac_Click(object sender, EventArgs e)
        {            
            radForm_CRM_NhatKyLienLac form = new radForm_CRM_NhatKyLienLac();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiBaoGia_Click(object sender, EventArgs e)
        {
            radForm_CRM_BaoGia_ChiTiet form = new radForm_CRM_BaoGia_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachBaoGia_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachBaoGia form = new radForm_CRM_DanhSachBaoGia();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_BangGiaChiTieu_Click(object sender, EventArgs e)
        {
            radForm_CRM_BangBaoGiaChiTieu form = new radForm_CRM_BangBaoGiaChiTieu();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachHopDong_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachHopDong form = new radForm_CRM_DanhSachHopDong();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiHopDong_Click(object sender, EventArgs e)
        {
            radForm_CRM_HopDong_ChiTiet form = new radForm_CRM_HopDong_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiPhieuNhanMau_Click(object sender, EventArgs e)
        {
            radForm_CRM_PhieuNhanMau_ChiTiet form = new radForm_CRM_PhieuNhanMau_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachPhieuNhanMau_Click(object sender, EventArgs e)
        {
            radForm_KiemNghiem_DanhSachTachMau form = new radForm_KiemNghiem_DanhSachTachMau();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachMauDaLuu_Click(object sender, EventArgs e)
        {
            radForm_KiemNghiem_DanhSachMauDaLuu form = new radForm_KiemNghiem_DanhSachMauDaLuu();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachMauTiepNhan_Click(object sender, EventArgs e)
        {
            radForm_KiemNghiem_DanhSachMauTiepNhan form = new radForm_KiemNghiem_DanhSachMauTiepNhan();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_LapKeHoachKiemNghiem_Click(object sender, EventArgs e)
        {
            radForm_KiemNghiem_KeHoachKiemNghiem_ChiTiet form = new radForm_KiemNghiem_KeHoachKiemNghiem_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_CongViecCuaNhanSuTaiPhongBan_Click(object sender, EventArgs e)
        {
            radForm_KiemNghiem_NhanSuPhongThiNghiem form = new radForm_KiemNghiem_NhanSuPhongThiNghiem();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachCongNo_Click(object sender, EventArgs e)
        {
            radForm_CongNo_DanhSachCongNo form = new radForm_CongNo_DanhSachCongNo();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_ThietLapQuanLyCongNo_Click(object sender, EventArgs e)
        {
            radForm_CongNo_TheoDoiThayDoiCongNo form = new radForm_CongNo_TheoDoiThayDoiCongNo();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachTaiSan_Click(object sender, EventArgs e)
        {
            radForm_TaiSan_DanhSachTaiSan form = new radForm_TaiSan_DanhSachTaiSan();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_NhapTaiSan_Click(object sender, EventArgs e)
        {
            radForm_TaiSan_NhapMoiTaiSan form = new radForm_TaiSan_NhapMoiTaiSan();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachDotKiemKe_Click(object sender, EventArgs e)
        {
            radForm_TaiSan_DanhSachDotKiemKe form = new radForm_TaiSan_DanhSachDotKiemKe();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoDotKiemKe_Click(object sender, EventArgs e)
        {
            radForm_TaiSan_DotKiemKe_ChiTiet form = new radForm_TaiSan_DotKiemKe_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DieuChinhSauKiemKe_Click(object sender, EventArgs e)
        {
            radForm_TaiSan_DieuChinhSauKiemKe form = new radForm_TaiSan_DieuChinhSauKiemKe();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachCongViec_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachCongViec form = new radForm_CRM_DanhSachCongViec();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiCongViec_Click(object sender, EventArgs e)
        {
            radForm_CRM_CongViec_ChiTiet form = new radForm_CRM_CongViec_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachGhiChuCongNo_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachCongViec form = new radForm_CRM_DanhSachCongViec();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiGhiChuCongNo_Click(object sender, EventArgs e)
        {
            radForm_CRM_CongViec_ChiTiet form = new radForm_CRM_CongViec_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_DanhSachGhiChuTaiSan_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachCongViec form = new radForm_CRM_DanhSachCongViec();
            form.MdiParent = this;
            form.Show();
        }

        private void radButtonElement_TaoMoiGhiChuTaiSan_Click(object sender, EventArgs e)
        {
            radForm_CRM_CongViec_ChiTiet form = new radForm_CRM_CongViec_ChiTiet();
            form.MdiParent = this;
            form.Show();
        }

        private void contextualTabGroup1_Click(object sender, EventArgs e)
        {

        }

        static string test = "";
        private void Main_KeyDown(object sender, KeyEventArgs e)
        {            
            if (e.KeyCode == Keys.F1)
            {
                Application.UseWaitCursor = true;
                this.radDesktopAlert1.AutoClose = true;
                this.radDesktopAlert1.AutoCloseDelay = 2;
                this.radDesktopAlert1.FixedSize = new Size(400, 100);
                this.radDesktopAlert1.PopupAnimationFrames = 5;
                this.radDesktopAlert1.FadeAnimationFrames = 5;
                this.radDesktopAlert1.Opacity = 1;
                
                this.radDesktopAlert1.CaptionText = "Bạn có công việc mới trong ngày";
                this.radDesktopAlert1.ContentText = "Gọi điện cho Anh Hùng - báo cáo tình hình doanh thu trong năm 2012."+ test;
                this.radDesktopAlert1.Show();
                this.radDesktopAlert1.Closed += new RadPopupClosedEventHandler(radDesktopAlert_Closed);
            }
        }

        void radDesktopAlert_Closed(object sender, RadPopupClosedEventArgs args)
        {
            Application.UseWaitCursor = false;
        }

        
        private void radTextBoxElement_GiaTriTimKiemDichVuKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Main.test = radTextBoxElement_GiaTriTimKiemDichVuKhachHang.Text;
            }
        }

        private void CloseForm()
        {
            this.Close();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsWarningClosing)
                if (MessageBox.Show("Bạn có muốn thoát khỏi phiên làm việc hiện nay và đóng lại chương trình hay không?", "Đóng chương trình", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    e.Cancel = true;
        }
    }
}
